import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'
import path from 'path'

console.log(process.env.ELECTRON_RENDERER_URL)

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    base: './',
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src'),
        '@': '/src',
        '@/assets': '/src/assets',
        '@/components': '/src/components',
        '@/pages': '/src/pages',
        '@/utils': '/src/utils',
        '@/hooks': '/src/hooks',
        '@/store': '/src/store',
        '@/config': '/src/config',
        '@/api': '/src/api',
        '@/router': '/src/router',
        '@/layouts': '/src/layouts',
        '@/styles': '/src/styles',
        '@/types': '/src/types',
        '@/locale': '/src/locale',
        '@/bug-manager': '/src/apps/bug-manager',
        }
    },
    plugins: [react()],
    // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
    // base: './',
    // 1. prevent vite from obscuring rust errors
    clearScreen: false,
    // 2. tauri expects a fixed port, fail if that port is not available
    server: {
      port: 5080,
      strictPort: true,
      host: 'localhost',
      proxy: {
        '/jsy': {
          target: 'https://saas.jishiyuchat.com',
          changeOrigin: true,
          rewrite: (path: string) => path.replace(/^\/jsy/, '/jsy'),
          bypass(req: any, res: any, options: any) {
            //@ts-ignore
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || ''
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl', proxyUrl)
          },
        },
        '/tongtool': {
          target: 'https://shiming.tongtool.com',
          changeOrigin: true,
          rewrite: (path: string) => path.replace(/^\/tongtool/, ''),
          bypass(req: any, res: any, options: any) {
            //@ts-ignore
            const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || ''
            req.headers['x-req-proxyUrl'] = proxyUrl
            res.setHeader('x-res-proxyUrl', proxyUrl)
          },
        }
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "${path.resolve(__dirname, './src/renderer/src/styles/var.less')}"; @import "${path.resolve(__dirname, './src/renderer/src/styles/global-color.less')}";`,
        }
      }
    },
  }
})
